package com.datareport.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.datareport.data.dto.ResultDTO;
import com.datareport.data.entity.Role;
import com.datareport.data.entity.UserRole;
import com.datareport.data.mapper.RoleMapper;
import com.datareport.data.mapper.UserRoleMapper;
import com.datareport.data.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DataReport
 * @since 2021-11-17
 */
@RestController
public class RoleController {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleService roleService;

    /**
     * 超级管理员分配权限接口
     * @param userId
     * @return
     */
    @ApiOperation("分配权限接口")
    @PreAuthorize("hasRole('ROLE_super_admin')")
    @GetMapping("/assign_permission")
    public Object assignPermission(@RequestParam("id")Long userId){


        UserRole userRole = new UserRole();

        //在用户-角色关联表中设置用户id
        userRole.setUserId(userId);

        Role admin = roleService.selectAdmin();

        userRole.setRoleId(admin.getId());

        //用户-角色关联表中，完成用户与角色的关联
        userRoleMapper.insert(userRole);

        return ResultDTO.okOf();
    }

}
